//
//  BoxSP.m
//  verletRopeTestProject
//
//  Created by Goffredo Marocchi on 12/6/10.
//  Copyright 2010 IGGS. All rights reserved.
//
#import "BoxSP.h"
#import "CoreGraphicsExt.h"
#import "GeneralDefines.h"


@implementation BoxSP

@synthesize box2dBody, oldPos;

- (CGRect)rectInPixels
{
	[super texture];
	CGSize s = [[super texture] contentSizeInPixels];
	return CGRectMake(-s.width / 2, -s.height / 2, s.width, s.height);
}

- (CGRect)rect
{
	CGSize s = [[super texture]  contentSize];
	return CGRectMake(-s.width / 2, -s.height / 2, s.width, s.height);
}

+(id) boxWithBatchNode:(CCSpriteBatchNode*)batchNode rect:(CGRect)rectangle {
	return [[[self alloc] spriteWithBatchNode:batchNode rect:rectangle] autorelease];
}
-(id) spriteWithBatchNode:(CCSpriteBatchNode*)batchNode rect:(CGRect)rectangle {
	
	if ((self = [super initWithBatchNode:batchNode rect:rectangle]) ) {
		
		state = kBoxStateUngrabbed;
		oldPos = self.position;
	}
	
	return self;
}

- (void)onEnter
{
	[[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:0 swallowsTouches:YES];
	[super onEnter];
}

- (void)onExit
{
	[[CCTouchDispatcher sharedDispatcher] removeDelegate:self];
	[super onExit];
}	

- (BOOL)containsTouchLocation:(UITouch *)touch
{
	CGPoint p = [self convertTouchToNodeSpaceAR:touch];
	CGRect r = [self rectInPixels];
	return CGRectContainsPoint(r, p);
}

- (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event
{
	if (state != kBoxStateUngrabbed) return NO;
	//NSLog(@"touching box...1?...");
	CGPoint p = [self convertTouchToNodeSpaceAR:touch];
	CGRect r = [self rectInPixels];
	
	CGPoint touchPoint = [touch locationInView:[touch view]];
	touchPoint = [[CCDirector sharedDirector] convertToUI:touchPoint];
	oldPos = touchPoint;
	
	//NSLog(@"touch (x,y) = (%.2f,%.2f)", p.x, p.y);
	//NSLog(@"sprite (x,y) = (%.2f,%.2f)", r.origin.x, r.origin.y);
	
	if ( ![self containsTouchLocation:touch] ) return NO;
	
	//NSLog(@"touching box...Rope");
	
	self.box2dBody->SetAngularVelocity(0);
	self.box2dBody->SetLinearVelocity(b2Vec2(0.0f,0.0f));
	
	state = kBoxStateGrabbed;
	return YES;
}

- (void)ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event
{
	// If it weren't for the TouchDispatcher, you would need to keep a reference
	// to the touch from touchBegan and check that the current touch is the same
	// as that one.
	// Actually, it would be even more complicated since in the Cocos dispatcher
	// you get NSSets instead of 1 UITouch, so you'd need to loop through the set
	// in each touchXXX method.
	
	//NSAssert(state == kBoxStateGrabbed, @"Box - Unexpected state!");
	//NSLog(@"moving box...");
	
	CGPoint touchPoint = [touch locationInView:[touch view]];
	touchPoint = [[CCDirector sharedDirector] convertToUI:touchPoint];
	
	CGFloat maxDistBodies = CGDistanceBetweenPoints(touchPoint, self.oldPos);
	
	if (maxDistBodies > 215.0f) {
		[self ccTouchEnded:touch withEvent:event];
		//[[CCDirector sharedDirector] ];
		//return;
	}
	
	CGPoint distVector = CGVectorBetweenPoints(touchPoint, oldPos);
	
	distVector.x *= 32;
	distVector.y *= 32;
	
	CGFloat d = CGDistanceBetweenPoints(touchPoint, oldPos);
	
	d = CLAMP_VAL(d, 0.1f, 0.2f);
	//NSLog(@"d = %f", d);
	
	self.box2dBody->SetAngularVelocity(0);

	self.box2dBody->SetLinearVelocity(b2Vec2(distVector.x/PTM_RATIO, distVector.y/PTM_RATIO));	
	oldPos = touchPoint;
}

- (void)ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event
{
	//NSAssert(state == kBoxStateGrabbed, @"Paddle - Unexpected state!");	
	
	state = kBoxStateUngrabbed;
	self.box2dBody->SetAngularVelocity(0);
	self.box2dBody->SetLinearVelocity(b2Vec2(0/PTM_RATIO, 0/PTM_RATIO));
}

@end
